package com.example.applets.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.applets.entity.Type;
import com.example.applets.service.impl.TypeServiceImpl;
import com.example.applets.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.*;

/**
 * <p>
 * 商品分类 前端控制器
 * </p>
 *
 * @author 云烟成雨丶
 * @since 2020-10-09
 */
@Controller
@RequestMapping("/applets/type")
@Configuration
public class TypeController {
    @Autowired
    private TypeServiceImpl typeService;

    @Value("${file.path}")
    private String paths;

    @RequestMapping("/page")
    @ResponseBody
    //商品分类展示
    public AjaxResult selectPage(@RequestParam(value = "pageNo", defaultValue = "1") int pageNo, @RequestParam(value = "pageSize", defaultValue = "5") int pageSize, @RequestParam(value = "parent") String parentid, @RequestParam(value = "typename") String typename) {
        //分页
        Page<Type> typePage = new Page<>(pageNo, pageSize);
        QueryWrapper<Type> wrapper = new QueryWrapper<>();
        //条件构造器
        if (!parentid.equals("")) {
            wrapper.eq("parentid", Long.parseLong(parentid));
        }
        if (!typename.equals("")) {
            wrapper.like("typename", typename);
        }
        wrapper.eq("inuse", 1);
        //获得List集合
        List<Type> list = typeService.page(typePage, wrapper).getRecords();
        //获得总数
        int count = (typeService.count(wrapper) - 1) / pageSize + 1;
        //创建map集合
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("list", list);
        map.put("total", count);
        if (list.size() == 0)
            return AjaxResult.error("暂无数据");
        return AjaxResult.success(map);
    }

    @RequestMapping("/add")
    @ResponseBody
    //增加商品种类
    public AjaxResult addType(@RequestParam(value = "iconcard", required = false) MultipartFile picture, Type type) throws IOException {
        if (!picture.isEmpty()) {
            //起新名字
            String oldName = picture.getOriginalFilename();
            String prefix = oldName.substring(oldName.lastIndexOf('.'));
            String newName = UUID.randomUUID() + prefix;
            //修改新名字
            type.setIcon(newName);
            //确定路径
            String path = paths;
            //修改存储路径
            picture.transferTo(new File(path, newName));
        }
        if (!typeService.save(type))
            return AjaxResult.error("保存失败!");
        return AjaxResult.success("保存成功!");
    }

    @RequestMapping("/select")
    //查找单个种类
    public String selectType(@RequestParam("id") Long id, Model model) {
        Type type = typeService.getById(id);
        model.addAttribute("t", type);
        return "ktype/UpdateType";
    }

    @RequestMapping("/update")
    @ResponseBody
    //修改种类
    public AjaxResult updateType(@RequestParam(value = "iconcard", required = false) MultipartFile picture, Type type) throws IOException {
        if (!picture.isEmpty()) {
            //起新名字
            String oldName = picture.getOriginalFilename();
            String prefix = oldName.substring(oldName.lastIndexOf('.'));
            String newName = UUID.randomUUID() + prefix;
            //修改新名字
            type.setIcon(newName);
            //确定路径
            String path = "F://picture//";
            //修改存储路径
            picture.transferTo(new File(path, newName));
        }
        if (!typeService.updateById(type))
            return AjaxResult.error("修改失败!");
        return AjaxResult.error("修改成功!");
    }

    @RequestMapping("/getParent")
    @ResponseBody
    //获取所有
    public AjaxResult getParent() {
        return AjaxResult.success(typeService.list());
    }

    @RequestMapping("/updateParent")
    @ResponseBody
    //更改父类
    public AjaxResult updateParent(@RequestParam("arrType") String arrType, @RequestParam("parentid") long parentid) {
        String[] str = arrType.split("-");
        List<Type> list = new ArrayList<>();
        for (String s : str) {
            Type type = typeService.getById(Long.parseLong(s));
            type.setParentid(parentid);
            list.add(type);
        }
        if (!typeService.updateBatchById(list))
            return AjaxResult.error("修改失败!");
        return AjaxResult.error("修改成功!");
    }

    @RequestMapping("/getOnePar")
    @ResponseBody
    //获取单个父类
    public AjaxResult getParent(@RequestParam("parentid") long id) {
        Type type = typeService.getById(id);
        if (type == null)
            return AjaxResult.error("null", "null");
        return AjaxResult.success("成功", type.getTypename());
    }
}

